package com.perseygroup.apps.module.edt;

import java.util.Calendar;

import android.content.SharedPreferences;
import android.content.res.Resources;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.perseygroup.apps.GeneralActivity;
import com.perseygroup.apps.R;

public class ModuleEdt extends GeneralActivity {
	
	protected static final int MONTHYEARDATESELECTOR_ID = 0;
	
	protected TextView monthYearText;
	protected GridView gridview;
	protected ImageView flecheGauche, flecheDroite;
	
	//Prefs Objects
	protected static final String PREFS_CALENDRIER = "MyPerseyCalendarPrefs";//Save my calendar prefs
	protected static final Calendar monthYearNow = Calendar.getInstance();//Get the instance of the calendar now
	protected static String PREFS_MOIS_CHOISI = "PREFS_MOIS_CHOISI";//Save my calendar prefs Month
	protected static String PREFS_ANNEE_CHOISIE = "PREFS_ANNEE_CHOISIE";//Save my calendar prefs Year
	
	protected static String PREFS_DATECHOOSEINCALENDAR_JOUR = "PREFS_DATECHOOSEINCALENDAR_JOUR";
	protected static String PREFS_DATECHOOSEINCALENDAR_MOIS = "PREFS_DATECHOOSEINCALENDAR_MOIS";
	protected static String PREFS_DATECHOOSEINCALENDAR_ANNEE = "PREFS_DATECHOOSEINCALENDAR_ANNEE";
	
	protected SharedPreferences myPerseyCalendarPrefs;
	
	//Set Month and Year Store in Prefs
	protected void setPrefsMonthYearChoose(int M, int Y)
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		SharedPreferences.Editor prefsEditor = this.myPerseyCalendarPrefs.edit();
		prefsEditor.putInt(PREFS_MOIS_CHOISI, M);//Set Month Choose
		prefsEditor.putInt(PREFS_ANNEE_CHOISIE, Y);//Set Year Choose
		prefsEditor.commit();
	}
	
	//Set The day choosen from calendar in prefs
	protected void setDateChoosenInCalendar(int J, int M, int Y)
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		SharedPreferences.Editor prefsEditor = this.myPerseyCalendarPrefs.edit();
		prefsEditor.putInt(PREFS_DATECHOOSEINCALENDAR_JOUR, J);//Set Day Choose
		prefsEditor.putInt(PREFS_DATECHOOSEINCALENDAR_MOIS, M);//Set Month Choose
		prefsEditor.putInt(PREFS_DATECHOOSEINCALENDAR_ANNEE, Y);//Set Year Choose
		prefsEditor.commit();
	}
	
	//Get Day, Month and Year choosen in calendar
	protected int[] getDayInCalendarAndPrefs()
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		int dayChoose = this.myPerseyCalendarPrefs.getInt(PREFS_DATECHOOSEINCALENDAR_JOUR, -1);
		int monthChoose = this.myPerseyCalendarPrefs.getInt(PREFS_DATECHOOSEINCALENDAR_MOIS, -1);
		int yearChoose = this.myPerseyCalendarPrefs.getInt(PREFS_DATECHOOSEINCALENDAR_ANNEE, -1);
		int[] val = { dayChoose, monthChoose, yearChoose };//0-Month, 1-Year
		return val;
	}
	
	//Get Month and Year Store in Prefs
	protected int[] getPrefsMonthYearChoose()
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		int prefMonthChoose = this.myPerseyCalendarPrefs.getInt(PREFS_MOIS_CHOISI, -1);
		int prefYearChoose = this.myPerseyCalendarPrefs.getInt(PREFS_ANNEE_CHOISIE, -1);
		int[] val = { prefMonthChoose, prefYearChoose };//0-Month, 1-Year
		return val;
	}
	
	//Clean the Edt Prefs Data when finish the activity EdtGridActivity
	protected void cleanEdtPrefsData()
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		SharedPreferences.Editor prefsEditor = this.myPerseyCalendarPrefs.edit();
		prefsEditor.clear();
		prefsEditor.commit();
	}
	
	protected void moveCalendrierForward()
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		int prefMonthChoose = this.myPerseyCalendarPrefs.getInt(PREFS_MOIS_CHOISI, -1);
		int prefYearChoose = this.myPerseyCalendarPrefs.getInt(PREFS_ANNEE_CHOISIE, -1);
		
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, prefMonthChoose-1);
		c.set(Calendar.YEAR, prefYearChoose);
		c.add(Calendar.MONTH, 1);
		setPrefsMonthYearChoose((c.get(Calendar.MONTH)+1), c.get(Calendar.YEAR));
	}
	
	protected void moveCalendrierBackward()
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		int prefMonthChoose = this.myPerseyCalendarPrefs.getInt(PREFS_MOIS_CHOISI, -1);
		int prefYearChoose = this.myPerseyCalendarPrefs.getInt(PREFS_ANNEE_CHOISIE, -1);
		
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, prefMonthChoose-1);
		c.set(Calendar.YEAR, prefYearChoose);
		c.add(Calendar.MONTH, -1);
		
		setPrefsMonthYearChoose((c.get(Calendar.MONTH)+1), c.get(Calendar.YEAR));
	}
	
	protected String[] getJoursHeaderCalendar()
	{
		Resources res = getResources();
		String[] listeJoursSemaaine = res.getStringArray(R.array.jours_header_calendar);
		return listeJoursSemaaine;
	}
	
	protected String getJourSemaine(int nbreJour)
	{
		Resources res = getResources();
		String[] jourssemaine = res.getStringArray(R.array.jours_semaine);
		return jourssemaine[nbreJour-1];
	}
	
	protected int[] getMyFirstDayOfTheWeek(int jour, int mois, int annee)
	{
		Calendar dateChoose = Calendar.getInstance();
        dateChoose.set(Calendar.DAY_OF_MONTH, jour);
        dateChoose.set(Calendar.MONTH, mois);
        dateChoose.set(Calendar.YEAR, annee);
        
        while (dateChoose.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        	dateChoose.add(Calendar.DAY_OF_WEEK,-1);
       }
       
        int[] myFirstDayOfWeek = {dateChoose.get(Calendar.DAY_OF_MONTH), dateChoose.get(Calendar.MONTH), dateChoose.get(Calendar.YEAR)};
        
        return myFirstDayOfWeek;
	}
}